using Ninject;
using Ninject.Modules;
using Ninject.Web.Common;
using SmallJob.Data;
using SmallJob.Data.Interfaces;
using SmallJob.Services.Implements;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Implements;
using SmallShop.Services.Interfaces;
using System;
using System.Web.Mvc;
using System.Web.Routing;

namespace FrontEnd
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode,
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Root", // Route name
                "", // URL with parameters
                new { controller = "Home", action = "Index", username = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Vip", // Route name
                "vip", // URL with parameters
                new { controller = "Payment", action = "Premium"} // Parameter defaults
            );
            routes.MapRoute(
                "Sua Yeu Cau", // Route name
                "sua-yeu-cau/{id}", // URL with parameters
                new { controller = "Request", action = "Add", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Them Yeu Cau", // Route name
                "them-yeu-cau", // URL with parameters
                new { controller = "Request", action = "Add" } // Parameter defaults
            );
            routes.MapRoute(
                "Yeu Cau", // Route name
                "yeu-cau/{id}", // URL with parameters
                new { controller = "request", action = "details", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "liet ke Yeu Cau", // Route name
                "cho-yeu-cau", // URL with parameters
                new { controller = "request", action = "show", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Hop thu", // Route name
                "hom-thu-ca-nhan", // URL with parameters
                new { controller = "personal", action = "inbox" } // Parameter defaults
            );
            routes.MapRoute(
                "Quan Ly Dat Hang", // Route name
                "dat-hang", // URL with parameters
                new { controller = "order", action = "manageorder"} // Parameter defaults
            );
            routes.MapRoute(
                "Quan Ly Cong Viec", // Route name
                "cong-viec", // URL with parameters
                new { controller = "order", action = "manageservice" } // Parameter defaults
            );
            routes.MapRoute(
                "Don Hang", // Route name
                "don-hang/{id}", // URL with parameters
                new { controller = "order", action = "details", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Dich Vu", // Route name
                "dich-vu/{id}", // URL with parameters
                new { controller = "Provide", action = "Details", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Danh Muc", // Route name
                "danh-muc/{id}", // URL with parameters
                new { controller = "Provide", action = "Category", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Dia Diem", // Route name
                "dia-diem/{id}", // URL with parameters
                new { controller = "Provide", action = "Location", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Tim kiem", // Route name
                "tim-kiem/{id}", // URL with parameters
                new { controller = "Provide", action = "Search", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Sua Dich Vu", // Route name
                "sua-dich-vu/{id}", // URL with parameters
                new { controller = "Provide", action = "Add", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Them Dich Vu", // Route name
                "them-dich-vu", // URL with parameters
                new { controller = "Provide", action = "Add" } // Parameter defaults
            );
            routes.MapRoute(
                "Error 404", // Route name
                "Error-404", // URL with parameters
                new { controller = "Error", action = "Error404" } // Parameter defaults
            );
            routes.MapRoute(
                "Ducnt", // Route name
                "{username}", // URL with parameters
                new { controller = "User", action = "Index", username = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            GlobalFilters.Filters.Add(new HandleErrorAttribute());
        }

        protected override IKernel CreateKernel()
        {
            var modules = new INinjectModule[]
            {
                new ServiceModule()
            };

            return new StandardKernel(modules);
        }
    }

    internal class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IMMessageService>().To<MMessageService>();
            Bind<IMConversationService>().To<MConversationService>();
            Bind<ITransactionService>().To<TransactionService>();
            Bind<ISmsLogService>().To<SmsLogService>();
            Bind<IFollowerService>().To<FollowerService>();
            Bind<IEmailFormatService>().To<EmailFormatService>();
            Bind<IFeatureService>().To<FeatureService>();
            Bind<IBannerService>().To<BannerService>();
            Bind<IMessageService>().To<MessageService>();
            Bind<ICategoryService>().To<CategoryService>();
            Bind<IBidRequestService>().To<BidRequestService>();
            Bind<IRequestService>().To<RequestService>();
            Bind<IProvideService>().To<ProvideService>();
            Bind<IOrderService>().To<OrderService>();
            Bind<IRequestCommentService>().To<RequestCommentService>();
            Bind<IProvideCommentService>().To<ProviceCommentService>();
            Bind<IOfferService>().To<OfferService>();
            Bind<IOfferMessageService>().To<OfferMessageService>();
            Bind<ISkillService>().To<SkillService>();
            Bind<IDatabaseFactory>().To<DatabaseFactory>();
            Bind<IUserService>().To<UserService>();
            Bind<IDisposable>().To<Disposable>();
        }
    }
}